/**
 * Created by Administrator on 2017/4/18.
 */
// 基于moment的扩展date方法操作
export const getFormatAgeByBirthday = (birthday) => {
  if (!birthday) {
    return '0岁'
  }
  let birthYear = birthday.substr(0, 4)
  let birthMonth = birthday.substr(4, 2)
  let birthDay = birthday.substr(6, 2)

  let birhDate = new Date()
  birhDate.setFullYear(birthYear)
  birhDate.setMonth(birthMonth - 1)
  birhDate.setDate(birthDay)

  let dateNow = new Date()
  let nowYear = dateNow.getFullYear()
  let nowMonth = dateNow.getMonth() + 1

  if (birhDate.getTime() > dateNow.getTime()) {
    return '0岁'
  }
  // 计算年龄
  if (birthYear === nowYear) { // 同年
    if (birthMonth === nowMonth) {
      return '0月'
    }
  } else { // 不同年
    if (nowMonth >= birthMonth) { // 当前月份大于生日月份
      return `${nowYear - birthYear}年Z${nowMonth - birthMonth}月`
    } else {
      return `${nowYear - birthYear - 1 > 0 ? (nowYear - birthYear - 1) + '岁' : ''}${nowMonth + 12 - birthMonth}月`
    }
  }
}